import { queryDeviceCountUsed, queryDeviceCountOFF, queryPowerList, queryTemplateList, queryHumidnessList, queryDeviceCountON , queryElectricityList } from "../dao/pageDao2.js";


import {getDateStr} from "./pageController1.js"

function getDeviceCountUsed(req, res) {
    console.log('调用了 getDeviceCountUsed');
    queryDeviceCountUsed(function (result) {
        let resultJson = {
            data: result[0].total
        }
        res.writeHead(200);
        res.write(JSON.stringify(resultJson));
        res.end();
    });
}

function getDeviceCountOFF(req, res) {
    console.log('调用了 getDeviceCountOFF');
    queryDeviceCountOFF(function (result) {
        let resultJson = {
            data: result[0].total
        }
        res.writeHead(200);
        res.write(JSON.stringify(resultJson));
        res.end();
        console.log(result);
    });
}

function getDeviceCountON(req, res) {
    console.log('调用了 getDeviceCountOFF');
    queryDeviceCountON(function (result) {
        let resultJson = {
            data: result[0].total
        }
        res.writeHead(200);
        res.write(JSON.stringify(resultJson));
        res.end();
        console.log(result);
    });
}

function getDateStr2(time){
    return time.split('.')[0]
}

function getPowerList(req, res) {
    console.log('调用了 getPrecinctCount');
    queryPowerList(function (result) {
        let nameList = ['预测上限', '预测下限'];
        let timeList = [];
        let maxObj = {};
        let minObj = {};
        let dataObj = {}
        result.forEach(element => {
            let timeStr = getDateStr2(element.time);
            if (nameList.includes(element.name)) { }
            else {
                nameList.push(element.name);
            }
            if (timeList.includes(timeStr)) { }
            else {
                timeList.push(timeStr);
            }
            // 预测上限
            if (timeStr in maxObj) {
                if (maxObj[timeStr] < element.power) {
                    maxObj[timeStr] = element.power
                }
            } else {
                maxObj[timeStr] = element.power
            }
            // 预测下限
            if (timeStr in minObj) {
                if (minObj[timeStr] > element.power) {
                    minObj[timeStr] = element.power
                }
            } else {
                minObj[timeStr] = element.power
            }
            if (element.name in dataObj) {

            } else {
                dataObj[element.name] = { data: [] }
            }
            dataObj[element.name][timeStr] = element.power

        });

        let maxList = [];
        let minList = [];
        timeList.forEach(k => {
            maxList.push(parseInt(maxObj[k]) + 2);
            minList.push(parseInt(minObj[k]) - 2);
        });
        var series = [{
            type: 'line',
            name: '预测上限',
            symbol: 'circle',
            symbolSize: 10,
            smooth: true,
            itemStyle: {
                normal: {
                    show: false,
                    color: '#00eef8',
                    opacity: 0
                },
                emphasis: {
                    color: '#00eef8',
                }
            },
            lineStyle: {
                normal: {
                    color: '#00eef8',
                }
            },
            data: maxList,
        },
        {
            type: 'line',
            name: '预测下限',
            symbol: 'circle',
            symbolSize: 10,
            smooth: true,
            itemStyle: {
                normal: {
                    color: '#ffbe0d',
                    opacity: 0
                },
                emphasis: {
                    color: '#ffbe0d',
                }
            },
            lineStyle: {
                normal: {
                    color: '#ffbe0d',
                }
            },
            data: minList,
        }]
        let limit = 0;
        for (var attr in dataObj) {
            timeList.forEach(k => {
                dataObj[attr].data.push(dataObj[attr][k]);
            });
            series.push({
                name: attr,
                type: 'line',
                smooth: true,
                data: [dataObj[attr].data[0]],
                dataBack: dataObj[attr].data
            });
            if (limit >= 4) {
                break;
            }
            limit++;
        }
        let resultJson = {
            series: series,
            nameList: nameList,
            timeList: timeList
        }
        res.writeHead(200);
        res.write(JSON.stringify(resultJson));
        res.end();
    });
}


function getTemplateList(req, res){
    console.log('调用了 getTemplateList');
    queryTemplateList(function (result) {
        let nameList = ['预测上限', '预测下限'];
        let timeList = [];
        let maxObj = {};
        let minObj = {};
        let dataObj = {}
        
        result.forEach(element => {
            let device_name = element.device_name.substr(0, element.device_name.length - 4);
            let timeStr = getDateStr(element.record_time);
            if (nameList.includes(device_name)) { }
            else {
                nameList.push(device_name);
            }
            if (timeList.includes(timeStr)) { }
            else {
                timeList.push(timeStr);
            }
            // 预测上限
            if (timeStr in maxObj) {
                if (maxObj[timeStr] < element.mete_value) {
                    maxObj[timeStr] = element.mete_value
                }
            } else {
                maxObj[timeStr] = element.mete_value
            }
            // 预测下限
            if (timeStr in minObj) {
                if (minObj[timeStr] > element.mete_value) {
                    minObj[timeStr] = element.mete_value
                }
            } else {
                minObj[timeStr] = element.mete_value
            }
            if (device_name in dataObj) {

            } else {
                dataObj[device_name] = { data: [] }
            }
            dataObj[device_name][timeStr] = element.mete_value

        });

        let maxList = [];
        let minList = [];
        timeList.forEach(k => {
            maxList.push(parseInt(maxObj[k]) + 2);
            minList.push(parseInt(minObj[k]) - 2);
        });
        var series = [{
            type: 'line',
            name: '预测上限',
            symbol: 'circle',
            symbolSize: 10,
            smooth: true,
            itemStyle: {
                normal: {
                    show: false,
                    color: '#00eef8',
                    opacity: 0
                },
                emphasis: {
                    color: '#00eef8',
                }
            },
            lineStyle: {
                normal: {
                    color: '#00eef8',
                }
            },
            data: maxList,
        },
        {
            type: 'line',
            name: '预测下限',
            symbol: 'circle',
            symbolSize: 10,
            smooth: true,
            itemStyle: {
                normal: {
                    color: '#ffbe0d',
                    opacity: 0
                },
                emphasis: {
                    color: '#ffbe0d',
                }
            },
            lineStyle: {
                normal: {
                    color: '#ffbe0d',
                }
            },
            data: minList,
        }]

        for (var attr in dataObj) {
            let last = 0;
            timeList.forEach(k => {
                if (k in dataObj[attr]){
                    last = dataObj[attr][k];
                }
                dataObj[attr].data.push(last);
            });

            series.push({
                name: attr,
                type: 'line',
                smooth: true,
                data: [dataObj[attr].data[0]],
                dataBack: dataObj[attr].data
            });

        }
        let resultJson = {
            series: series,
            nameList: nameList,
            timeList: timeList
        }
        res.writeHead(200);
        res.write(JSON.stringify(resultJson));
        res.end();
    });
}


function getHumidnessList(req, res){
    console.log('调用了 getHumidnessList');
    queryHumidnessList(function (result) {
        let nameList = ['预测上限', '预测下限'];
        let timeList = [];
        let maxObj = {};
        let minObj = {};
        let dataObj = {}
        
        result.forEach(element => {
            let device_name = element.device_name.substr(0, element.device_name.length - 4);
            
            let timeStr = getDateStr(element.record_time);
            if (nameList.includes(device_name)) { }
            else {
                nameList.push(device_name);
            }
            if (timeList.includes(timeStr)) { }
            else {
                timeList.push(timeStr);
            }
            // 预测上限
            if (timeStr in maxObj) {
                if (maxObj[timeStr] < element.mete_value) {
                    maxObj[timeStr] = element.mete_value
                }
            } else {
                maxObj[timeStr] = element.mete_value
            }
            // 预测下限
            if (timeStr in minObj) {
                if (minObj[timeStr] > element.mete_value) {
                    minObj[timeStr] = element.mete_value
                }
            } else {
                minObj[timeStr] = element.mete_value
            }
            if (device_name in dataObj) {

            } else {
                dataObj[device_name] = { data: [] }
            }
            dataObj[device_name][timeStr] = element.mete_value

        });

        let maxList = [];
        let minList = [];
        timeList.forEach(k => {
            maxList.push(parseInt(maxObj[k]) + 2);
            minList.push(parseInt(minObj[k]) - 2);
        });
        var series = [{
            type: 'line',
            name: '预测上限',
            symbol: 'circle',
            symbolSize: 10,
            smooth: true,
            itemStyle: {
                normal: {
                    show: false,
                    color: '#00eef8',
                    opacity: 0
                },
                emphasis: {
                    color: '#00eef8',
                }
            },
            lineStyle: {
                normal: {
                    color: '#00eef8',
                }
            },
            data: maxList,
        },
        {
            type: 'line',
            name: '预测下限',
            symbol: 'circle',
            symbolSize: 10,
            smooth: true,
            itemStyle: {
                normal: {
                    color: '#ffbe0d',
                    opacity: 0
                },
                emphasis: {
                    color: '#ffbe0d',
                }
            },
            lineStyle: {
                normal: {
                    color: '#ffbe0d',
                }
            },
            data: minList,
        }]
        let limit = 0;
        for (var attr in dataObj) {
            // timeList.forEach(k => {
            //     dataObj[attr].data.push(dataObj[attr][k]);
            // });
            let last = 0;
            timeList.forEach(k => {
                if (k in dataObj[attr]){
                    last = dataObj[attr][k];
                }
                dataObj[attr].data.push(last);
            });
            series.push({
                name: attr,
                type: 'line',
                smooth: true,
                data: [dataObj[attr].data[0]],
                dataBack: dataObj[attr].data
            });
            if (limit >= 4) {
                break;
            }
            limit++;
        }
        let resultJson = {
            series: series,
            nameList: nameList,
            timeList: timeList
        }
        res.writeHead(200);
        res.write(JSON.stringify(resultJson));
        res.end();
    });
}


function getElectricityList(req, res){
    console.log('调用了 getElectricityList');
    queryElectricityList(function (result) {
        let nameList = ['预测上限', '预测下限'];
        let timeList = [];
        let maxObj = {};
        let minObj = {};
        let dataObj = {}
        
        result.forEach(element => {
            
            // let timeStr = getDateStr(element.record_time);
            let timeStr = element.record_time;
            if (nameList.includes(element.device_name)) { }
            else {
                nameList.push(element.device_name);
            }
            if (timeList.includes(timeStr)) { }
            else {
                timeList.push(timeStr);
            }
            // 预测上限
            if (timeStr in maxObj) {
                if (maxObj[timeStr] < element.mete_value) {
                    maxObj[timeStr] = element.mete_value
                }
            } else {
                maxObj[timeStr] = element.mete_value
            }
            // 预测下限
            if (timeStr in minObj) {
                if (minObj[timeStr] > element.mete_value) {
                    minObj[timeStr] = element.mete_value
                }
            } else {
                minObj[timeStr] = element.mete_value
            }
            if (element.device_name in dataObj) {

            } else {
                dataObj[element.device_name] = { data: [] }
            }
            dataObj[element.device_name][timeStr] = element.mete_value

        });

        let maxList = [];
        let minList = [];
        timeList.forEach(k => {
            maxList.push(parseInt(maxObj[k]) + 2);
            minList.push(parseInt(minObj[k]) - 2);
        });
        var series = [{
            type: 'line',
            name: '预测上限',
            symbol: 'circle',
            symbolSize: 10,
            smooth: true,
            itemStyle: {
                normal: {
                    show: false,
                    color: '#00eef8',
                    opacity: 0
                },
                emphasis: {
                    color: '#00eef8',
                }
            },
            lineStyle: {
                normal: {
                    color: '#00eef8',
                }
            },
            data: maxList,
        },
        {
            type: 'line',
            name: '预测下限',
            symbol: 'circle',
            symbolSize: 10,
            smooth: true,
            itemStyle: {
                normal: {
                    color: '#ffbe0d',
                    opacity: 0
                },
                emphasis: {
                    color: '#ffbe0d',
                }
            },
            lineStyle: {
                normal: {
                    color: '#ffbe0d',
                }
            },
            data: minList,
        }]
        let limit = 0;
        for (var attr in dataObj) {
            // timeList.forEach(k => {
            //     dataObj[attr].data.push(dataObj[attr][k]);
            // });
            let last = 0;
            timeList.forEach(k => {
                if (k in dataObj[attr]){
                    last = dataObj[attr][k];
                }
                dataObj[attr].data.push(last);
            });
            series.push({
                name: attr,
                type: 'line',
                smooth: true,
                data: [dataObj[attr].data[0]],
                dataBack: dataObj[attr].data
            });
            if (limit >= 4) {
                break;
            }
            limit++;
        }
        let resultJson = {
            series: series,
            nameList: nameList,
            timeList: timeList
        }
        res.writeHead(200);
        res.write(JSON.stringify(resultJson));
        res.end();
    });
}

const path = new Map();
path.set('/getDeviceCountUsed', getDeviceCountUsed);
path.set('/getDeviceCountOFF', getDeviceCountOFF);
path.set('/getDeviceCountON', getDeviceCountON);
path.set('/getPowerList', getPowerList);
path.set('/getTemplateList', getTemplateList);
path.set('/getHumidnessList', getHumidnessList);
path.set('/getElectricityList', getElectricityList)

export { path };